﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marmat.Forms
{
    public partial class SearchControl
    {
        /// <summary>
        /// 
        /// </summary>
        public class FieldCollection : IList<ISearchControlField>
        {

            private readonly List<ISearchControlField> collection = new List<ISearchControlField>();

            /// <summary>
            /// 获取或设置指定索引处的元素。
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            public ISearchControlField this[int index] { get => collection[index]; set => collection[index] = value; }

            /// <summary>
            ///  获取 System.Collections.Generic.List`1 中包含的元素数。
            /// </summary>
            public int Count => collection.Count;

            bool ICollection<ISearchControlField>.IsReadOnly => false;

            /// <summary>
            ///  将对象添加到 System.Collections.Generic.List`1 的结尾处。
            /// </summary>
            /// <param name="column"></param>
            /// <returns></returns>
            public Field Add(System.Data.DataColumn column)
            {
                Field field = new Field();
                field.Name = column.ColumnName;
                field.Caption = column.Caption;
                field.DataType = column.DataType;
                this.Add(field);
                return field;
            }

            /// <summary>
            /// 将对象添加到 System.Collections.Generic.List`1 的结尾处。
            /// </summary>
            /// <param name="item"></param>
            public void Add(ISearchControlField item)
            {
                ISearchControlField source = this.collection.FirstOrDefault(x => x.Name == item.Name);
                if (source != null)
                {
                    int index = this.collection.FindIndex(x => x.Name == item.Name);
                    this.collection.Remove(source);
                    this.collection.Insert(index, item);
                }
                else
                    this.collection.Add(item);
            }

            /// <summary>
            /// 从 System.Collections.Generic.List`1 中移除所有元素。
            /// </summary>
            public void Clear()
            {
                this.collection.Clear();
            }

            /// <summary>
            ///   确定某元素是否在 System.Collections.Generic.List`1 中。
            /// </summary>
            /// <param name="item"></param>
            /// <returns></returns>
            public bool Contains(ISearchControlField item)
            {
                return this.collection.Exists(x => x.Name == item.Name);
            }

            /// <summary>
            /// 从目标数组的指定索引处开始，将整个 System.Collections.Generic.List`1 复制到兼容的一维数组。
            /// </summary>
            /// <param name="array"></param>
            /// <param name="arrayIndex"></param>
            void ICollection<ISearchControlField>.CopyTo(ISearchControlField[] array, int arrayIndex)
            {
                this.collection.CopyTo(array, arrayIndex);
            }

            /// <summary>
            /// 返回循环访问 System.Collections.Generic.List`1 的枚举数。
            /// </summary>
            /// <returns></returns>
            public IEnumerator<ISearchControlField> GetEnumerator()
            {
                return this.collection.GetEnumerator();
            }

            /// <summary>
            /// 返回循环访问 System.Collections.Generic.List`1 的枚举数。
            /// </summary>
            /// <returns></returns>
            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.collection.GetEnumerator();
            }

            /// <summary>
            ///  搜索指定的对象，并返回整个 System.Collections.Generic.List`1 中第一个匹配项的从零开始的索引。
            /// </summary>
            /// <param name="item"></param>
            /// <returns></returns>
            int IList<ISearchControlField>.IndexOf(ISearchControlField item)
            {
                return this.collection.IndexOf(item);
            }

            /// <summary>
            /// 将元素插入 System.Collections.Generic.List`1 的指定索引处。
            /// </summary>
            /// <param name="index"></param>
            /// <param name="item"></param>
            public void Insert(int index, ISearchControlField item)
            {
                this.collection.Insert(index, item);
            }

            /// <summary>
            /// 从 System.Collections.Generic.List`1 中移除特定对象的第一个匹配项。
            /// </summary>
            /// <param name="item"></param>
            /// <returns></returns>
            public bool Remove(ISearchControlField item)
            {
                return this.collection.Remove(item);
            }

            /// <summary>
            /// 移除 System.Collections.Generic.List`1 的指定索引处的元素。
            /// </summary>
            /// <param name="index"></param>
            public void RemoveAt(int index)
            {
                this.collection.RemoveAt(index);
            }
        }
    }
}
